package com.example.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.example.contant.MessageConstant;
import com.example.entity.PageResult;
import com.example.entity.QueryPageBean;
import com.example.entity.Result;
import com.example.pojo.Goods;
import com.example.pojo.GoodsCategory;
import com.example.service.CategoryAdminService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {
    @Reference
    private CategoryAdminService categoryAdminService;
    @GetMapping("/getall")
    public Result getall(){
        try{
            List<GoodsCategory> goodsCategoryList=categoryAdminService.findall();
            return new Result(true,MessageConstant.GET_CATEGORYINFO_SUCCESS,goodsCategoryList);
        }catch (Exception e){
            e.printStackTrace();
            return  new Result(false, MessageConstant.GET_CATEGORYINFO_FAIL);
        }
    }
    @PostMapping("/findpage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return categoryAdminService.findpage(queryPageBean);
    }
    @RequestMapping("/add")
    public Result add(@RequestBody GoodsCategory goodsCategory){
        try{
            categoryAdminService.add(goodsCategory);
        }catch (Exception e){
            e.printStackTrace();
            return  new Result(false,MessageConstant.ADD_CATEGORY_FAIL);
        }
        return new Result(true,MessageConstant.ADD_CATEGORY_SUCCESS);
    }
    @GetMapping("/delete")
    public Result delete(Integer id){

        try{
            categoryAdminService.deleteById(id);
        }catch (Exception e){
            e.printStackTrace();
            return  new Result(false,MessageConstant.DELETE_CATEGORY_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_CATEGORY_SUCCESS);
    }

    @PostMapping("/edit")
    public Result edit(@RequestBody GoodsCategory goodsCategory){
        try{
           categoryAdminService.edit(goodsCategory);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CATEGORY_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_CATEGORY_SUCCESS);

    }
    @GetMapping("/findById")
    public Result findById(Integer id){
        try{
           GoodsCategory goodsCategory=categoryAdminService.findById(id);
            return  new Result(true,MessageConstant.QUERY_CATEGORY_SUCCESS,goodsCategory);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CAYGORY_FAIL);
        }
    }
}
